// pages/register/register.js

var Zan = require('../../css/index')
var app = getApp()
Page(
  Object.assign( {},Zan.TopTips,{
  /**
   * 页面的初始数据
   */
  data: {
    name:'',
    stuNum:'',
    email:'',
    cardNum:'',
    cardPaw:'',
    // focus control variable
    nameFocus:false,
    stuNumFocus: false,
    emailFocus: false,
    cardNumFocus: false,

    update:0,
    needChange:false,
    canPlay:false,
    submitDisable:false,
    backDisable:false,
  },
    onLoad: function (options) {
      var that = this;
      var initInfo = {};
      var data;
      if (options.needChange == '1') {
        this.setData({update:1,needChange:true})
        // this.setData({ needChange: true });
        wx.request({
          url: app.globalData.baseUrl + '/getUserData',
          data: {
            openid: app.globalData.openid
          },
         success:function(res){
           var code=res.data['code']
           if(code==200){
             wx.showLoading({
               title: '加载中',
               mask:true
             })
             var result=res.data['result'];             
             that.setData({               
               stuNum:result['stuNum'],
               email:result['email'],
               cardNum:result['cardNum'],
               name: result['name'],               
             })
             setTimeout(function () { wx.hideLoading(); that.setData({ canPlay: true})},500);
           }
           else if(code==404){
             wx.showToast({
               title: '您尚未注册，现跳往注册',                                             
             })
             setTimeout(function(){
               wx.redirectTo({
                 url: 'pages/register/register',
               })
             },500)
           } 
         }
        })
      } //if end 
      else {
        this.setData({
          canPlay: true
        });
      }//else end
    },//onLoad end
  
  /*填写完毕 */
  submit: function(e){
    
    var info={}
    let value = e.detail.value;
    // let checkCode=value.checkNum.length;
    // if(checkCode==0){
    //   wx.showToast({
    //     title: '请输入验证码',
    //     image: '/images/ing.png'
    //   });
    //   return;
    // }
    let checkStuN = /^[0-9]{5,10}$/;
    let checkCardN = /^0[0-9]{5}$/;
    let checkEmail = /^[0-9]{2}[a-z]*[0-9]?$/;
    let flag=true;

    //测试姓名（随意）
    if (value.name.length == 0 ){
      // wx.showToast({
      //   title: '请输入姓名',
      //   image: '/images/ing.png'
      // });
      this.showZanTopTips('请输入姓名');
      this.setData({nameFocus:true});
      return;
    }
    if (value.name.length > 4){
      this.showZanTopTips('姓名有误，请重新输入');
      this.setData({ nameFocus: true });
      return;
    }
    this.data.stuNum=value.stuNum;
    this.setData({ nameFocus: false });
    
    //测试学号
    flag = checkStuN.test(value.stuNum);
    if(!flag){
      this.setData({stuNum:''}) 
      // wx.showToast({
      //   title: '学号输入有误',
      //   image: '/images/ing.png'
      // });
      this.showZanTopTips('学号输入有误');
      this.setData({ stuNumFocus: true });
      return;
    }
    
    //测试邮箱号
    this.data.email = value.email;
    this.setData({ emailFocus: false });
    flag = checkEmail.test(value.email);
    if (!flag) {
      this.setData({ email: '' })
      // wx.showToast({
      //   title: '邮箱输入有误',
      //   image: '/images/ing.png'
      // });
      this.showZanTopTips('邮箱输入有误');      
      this.setData({ emailFocus: true });
      return;
    }
    //测试一卡通号
    this.data.cardNum = value.cardNum;
    this.setData({ cardNumFocus: false });
    flag = checkCardN.test(value.cardNum);
    if (!flag) {
      this.setData({ carNum: '' })
      // wx.showToast({
      //   title: '一卡通号输入有误',
      //   image: '/images/ing.png'
      // });
      this.showZanTopTips('一卡通号输入有误');      
      this.setData({ cardNumFocus: true });
      return;
    }
    this.data.name = value.name;
    this.setData({ cardNumFocus: false });
    // this.data.cardPaw = value.cardPass;
    ;
    info={
      'name':value.name,
      'stuNum':value.stuNum,
      'email': value.email,
      'cardNum': value.cardNum,      
      'openid': app.globalData.openid,
      'update': this.data.update
    } 
    this.setData({submitDisable:true,backDisable:true});
    wx.request({
      url: app.globalData.baseUrl+'/regist',
      data:info,
      method:'POST',
      success:function(res){
        var code=res.data['code']
        console.log(code)
        if(code==1){
          wx.showToast({
            title: '注册成功',
            icon: 'success',
            success: function () {
              app.globalData.isRegisted= true;
              wx.redirectTo({
                url: '/pages/main/main',
              })
            }
          })
        }
        else if(code==200){
          wx.showToast({
            title: '修改成功',
            icon: 'success',
            success: function () {              
              setTimeout(function(){
                wx.navigateBack({
                  delta: 1
                })
              },1500)
            }
          })
        }
        else if(code==500){
          this.setData({ submitDisable: false, backDisable: false });          
          wx.showToast({
            title: '修改失败，请稍候再试',
            icon: 'none',
            success: function () {           
              setTimeout(function () {
                wx.navigateBack({
                  delta: 1
                })
              }, 1500)
            }
          })
        }
        else{
          this.setData({ submitDisable: fasle, backDisable: fasle });          
          wx.showToast({
            title: '服务器出现错误，请稍候再试',
            icon: 'none',
            success: function () {
              setTimeout(function () {
                wx.navigateBack({
                  delta: 1
                })
              }, 1500)
            }
          })
        }
        
      },
      fail:function(){
        wx.showToast({
          title: '服务器出现错误，请稍候再试',
          icon: 'none',
        })
        
      }
    })
  },
  back:function(){
   wx.showModal({
     title: '确定？',
     content: '确定放弃注册',
     showCancel: true,
     cancelText: '不注册',
    //  cancelColor: '',
     confirmText: '注册',
    //  confirmColor: '',
     success: function(res) {
       if(res.cancel){
         wx.redirectTo({
           url: '/pages/main/main',
         })
       }
     },
     fail: function(res) {},
     complete: function(res) {},
   })
  }
}))